package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.ProductMapper;
import com.fs.mapper.UserMapper;
import com.fs.pojo.Orders;
import com.fs.pojo.Product;
import com.fs.pojo.User;
import com.fs.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author hua
 * @version 1.0
 * @title ProductServiceImpl
 * @description 产品服务实现类
 * @create 2024/3/17 19:57
 */
@Service("ProductService")
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    /**
     * 根据商品名分页查询
     * @param pname 商品名
     * @param pageNum 页码
     * @return 产品集合
     */
    @Override
    public List<Product> showList(String pname,int pageNum) {
        IPage page = new Page(pageNum,6);
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(pname), Product::getPname,pname);
        productMapper.selectPage(page,queryWrapper);
        return page.getRecords();
    }

    /**
     * 根据商品名查产品数
     * @param pname 商品名
     * @return 产品数
     */
    @Override
    public Integer getTotal(String pname) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(pname),Product::getPname,pname);
        return productMapper.selectCount(queryWrapper);
    }

    /**
     * 根据商品id查产品
     * @param pid 商品id
     * @return 产品
     */
    @Override
    public Product showProduct(int pid) {
        return productMapper.selectById(pid);
    }
}
